<?php
class News extends MX_Controller
{
    function __construct(){
		parent::__construct();
	}
	
	function index(){
		$this->load->library('paging');
		$limit = 15;
		$this->load->model('news_model', 'news');
		$CategoryArray = $this->news->GetArrayCategory();
		$URI 	= $this->uri->segment(3);
		if(!$URI){
			$data['ListNews']	= $this->news->GetListForPaging(0, $limit);
			if(!empty($CategoryArray)){
				foreach($CategoryArray as $key=>$value){
					$data['List' . $key] = $this->news->GetListForPaging(0, 5, $key);
				}
			}
			$data['ListCategory'] = $this->news->GetListCategory();
			$this->layout->view('news', $data);
		} elseif(array_search($URI, $CategoryArray)){
			if($this->uri->segment(4)) {
				$page = $this->uri->segment(4);
			} else {
				$page = 1;
			}
			$CategoryID = array_search($URI, $CategoryArray);
			$this->paging->url 			= $this->config->item('base_url') . '/vi/news/' . $URI . '/';
			$this->paging->page_limit	= $limit;
			$this->paging->total_record	= $this->news->CountByCategory($CategoryID);
			$data['paging'] = $this->paging->CreateLink($page);
			$page = ($page - 1) * $limit;
			$data['ListNews']	= $this->news->GetListForPaging($page, $limit, $CategoryID);
			$this->layout->view('news_category', $data);
		} else {
			$NewsID 	= $this->uri->segment(3);
			$NewsCode 	= $this->uri->segment(4);
			$News		= $this->news->GetInfoByID($NewsID);
			if(empty($News)){
				header('Location:' . $this->config->item('base_url') . '/vi/news/' . $CategoryArray[1]);exit;
			} elseif($NewsCode != $News->Code) {
				header('Location:' . $this->config->item('base_url') . '/vi/news/' . $News->NewsID . '/' . $News->Code);exit;
			}
			$data['News']		= $News;
			$data['ListNews']	= $this->news->GetListForPaging(0, 5, $News->CategoryID);
			$this->layout->view('news_details', $data);
		}
	}
}
?>